package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.dto.DeviceClassifyDto;
import net.zigin.nhi.could.waste.model.DeviceClassify;
import net.zigin.nhi.could.waste.service.DeviceClassifyService;

import net.zigin.nhi.could.waste.vo.DeviceClassifyVo;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * DeviceClassify控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: DeviceClassifyController
 * @date 2021-9-12 17:47:15
 */
@Api(tags = "设备分类管理")
@RestController
@RequestMapping("/deviceClassify")
public class DeviceClassifyController extends BaseController {
    @Autowired
    private DeviceClassifyService deviceClassifyService;

    /**
     * 新增编辑设备分类
     *
     * @param deviceClassifyDto 设备分类信息
     * @return 设备分类ID
     */
    @ApiOperation("新增编辑设备分类")
    @ApiImplicitParam(name = "deviceClassifyDto", value = "设备分类信息", required = true, dataType = "DeviceClassifyDto")
    @PostMapping("/save")
    public JsonForResult<String> sava(@RequestBody DeviceClassifyDto deviceClassifyDto) {
        String id = deviceClassifyService.saveOrUpdate(deviceClassifyDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 设备分类列表
     *
     * @param deviceClassifyDto 设备分类信息
     * @return 设备分类列表
     */
    @ApiOperation("设备分类列表")
    @ApiImplicitParam(name = "deviceClassifyDto", value = "设备分类信息", required = true, dataType = "DeviceClassifyDto")
    @PostMapping("/getList")
    public JsonForResult<List<DeviceClassifyVo>> getList(@RequestBody DeviceClassifyDto deviceClassifyDto) {
        List<DeviceClassifyVo> list = deviceClassifyService.getList(deviceClassifyDto);
        Integer count = deviceClassifyService.getCount(deviceClassifyDto);
        return JsonForResult.<List<DeviceClassifyVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 设备分类详情
     *
     * @param id 设备分类ID
     * @return 设备分类详情
     */
    @ApiOperation("设备分类详情")
    @ApiImplicitParam(name = "id", value = "设备分类id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<DeviceClassifyVo> getById(@PathVariable String id) {
        DeviceClassify deviceClassify = deviceClassifyService.selectByPrimaryKey(id);
        DeviceClassifyVo deviceClassifyVo = ReflectUtil.copyProperties(deviceClassify, DeviceClassifyVo.class);
        return JsonForResult.<DeviceClassifyVo>builder().data(deviceClassifyVo).success(true).build();
    }

    /**
     * 删除设备分类
     *
     * @param id 设备分类ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除设备分类")
    @ApiImplicitParam(name = "id", value = "设备分类id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        if (deviceClassifyService.judgeRemove(id)) {
            deviceClassifyService.deleteByPrimaryKey(id);
            return JsonForResult.builder().success(true).build();
        } else {
            return JsonForResult.builder().data("该分类下有设备，不能删除！").success(false).build();
        }
    }

}
